package tank1;

import java.awt.*;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-31
 */
public class Bullet implements Runnable{
    private int x;
    private int y;
    private int direction;
    private Color color;
    private int time = 2;//子弹停顿时间
    private boolean isLive;//是否存在
    private int size = 5;//子弹大小

    @Override
    public void run() {
        while (isLive) {
            switch (direction) {
                //向上
                case 0:
                    y--;
                    break;
                //向下
                case 1:
                    y++;
                    break;
                //向左
                case 2:
                    x--;
                    break;
                //向右
                case 3:
                    x++;
                    break;
                default:
                    break;
            }
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (!(x>=0 && x<=TankFrame.WIDTH && y>=0 && y<=TankFrame.HEIGHT)) {
                isLive = false;
                //System.out.println("子弹线程终止" + Thread.currentThread().getName());
            }
            //System.out.println("(" + x + "," + y + ")" + "子弹" + Thread.currentThread().getName());
        }
    }

    public Bullet(int x, int y, int direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.isLive = true;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(int type) {
        if (type == 0) {
            color = Color.cyan;
        } else {
            color = Color.orange;
        }
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}
